package com.jandar.mvc.controller;

import com.jandar.mvc.dto.JDCommonResult;
import com.jandar.mvc.entity.JDBaseEntity;
import com.jandar.mvc.service.JDBaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class JDBaseController<Entity extends JDBaseEntity> {
    private final JDBaseService<Entity> service;

    public JDBaseController(JDBaseService<Entity> service) {
        this.service = service;
    }

    @PostMapping("findOne")
    @ApiOperation("条件查询一条数据")
    public JDCommonResult findByEntity(@RequestBody Entity entity){
        return JDCommonResult.success(service.findByEntity(entity));
    }
    @PostMapping("page")
    @ApiOperation("分页查询")
    public JDCommonResult page(@RequestBody Entity entity,@RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        Page<Entity> page = service.page(entity, auth);
        List<Entity> list = page.getContent();
        return JDCommonResult.success(list, Math.toIntExact(page.getTotalElements()));
    }

    @PostMapping("listBy")
    @ApiOperation("分页查询")
    public JDCommonResult listBy(@RequestBody Entity entity,@RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        return JDCommonResult.success(service.listBy(entity,auth));
    }
//    @GetMapping("list")
//    @ApiOperation("查询所有")
//    public JDCommonResult list() {
//        return JDCommonResult.success(service.list());
//    }

    @PostMapping("list")
    @ApiOperation("分页查询")
    public JDCommonResult list(@RequestBody Entity entity,@RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        Page<Entity> page = service.page(entity, auth);
        List<Entity> list = page.getContent();
        return JDCommonResult.success(list, Math.toIntExact(page.getTotalElements()));
    }

    @PostMapping("saveAll")
    @ApiOperation("批量新增/修改")
    public JDCommonResult saveAll(@RequestBody List<Entity> entities,@RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        return JDCommonResult.success(service.saveAll(entities,auth));
    }


    @PostMapping("add")
    @ApiOperation("新增")
    public JDCommonResult add(@RequestBody Entity entity, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        try {
            return JDCommonResult.success(service.save(entity,auth));
        } catch (Exception e) {
            return JDCommonResult.failed(e.getMessage());
        }
    }
    @PostMapping("save")
    @ApiOperation("新增/修改")
    public JDCommonResult save(@RequestBody Entity entity, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        try {
            return JDCommonResult.success(service.save(entity,auth));
        } catch (Exception e) {
            return JDCommonResult.failed(e.getMessage());
        }
    }
    @PostMapping("update")
    @ApiOperation("修改")
    public JDCommonResult update(@RequestBody Entity entity, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        try {
            return JDCommonResult.success(service.save(entity,auth));
        } catch (Exception e) {
            return JDCommonResult.failed(e.getMessage());
        }
    }

    @GetMapping("deleteById")
    @ApiOperation("通过id删除")
    public JDCommonResult deleteById(Integer id) {
        service.deleteById(id);
        return JDCommonResult.success("成功");
    }

    @PostMapping("delete")
    @ApiOperation("通过对象属性删除")
    public JDCommonResult delete(Entity entity) {
        service.delete(entity);
        return JDCommonResult.success("成功");
    }
    @GetMapping("delete/{id}")
    @ApiOperation("通过对象id删除")
    public JDCommonResult delete(@PathVariable("id") Integer id) {
        service.deleteById(id);
        return JDCommonResult.success("成功");
    }

}

